﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Drawing;
using System.Drawing.Imaging;

using OpenTK.Graphics.OpenGL;


namespace Platform359a.Core.Gfx
{
    class TextureManager
    {
        private Dictionary<string, int> mTextureLibrary;

        public TextureManager()
        {
            mTextureLibrary = new Dictionary<string, int>();
        }
        public void AddTexture(string pTextureName, string pTexturePath)
        {
            if (!System.IO.File.Exists(pTexturePath))
            {
                //! error cannot find texture file
            }
            if (Exists(pTextureName))
            {
                //! error texture exists
            }

            int tempTextureID = GL.GenTexture(); //create texture handle
            GL.BindTexture(TextureTarget.Texture2D, tempTextureID);

            Bitmap imageFile = new Bitmap(pTexturePath);
            BitmapData imageData = imageFile.LockBits(new Rectangle(0, 0, imageFile.Width, imageFile.Height), 
                                                      ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, imageData.Width, imageData.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
                          PixelType.UnsignedByte, imageData.Scan0);

            imageFile.UnlockBits(imageData);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            mTextureLibrary.Add(pTextureName, tempTextureID);

            GL.BindTexture(TextureTarget.Texture2D, 0);

        }
        public void RemoveTexture(string pTextureName)
        {
            if(!Exists(pTextureName))
            {
                //! warning texture does not exist
            }
            GL.DeleteTexture(mTextureLibrary[pTextureName]);
            mTextureLibrary.Remove(pTextureName);
        }
        public bool Exists(string pTextureName)
        {
            if (mTextureLibrary.ContainsKey(pTextureName))
            {
                return true;
            }
            return false;
        }
        public void ClearAll()
        {
            if (mTextureLibrary == null)
            {
                //! warning no textures
            }
            foreach (int feTexture in mTextureLibrary.Values)
            {
                GL.DeleteTexture(feTexture);
            }
            mTextureLibrary.Clear();
        }
        public int GetTexture(string pTextureName)
        {
            if(!Exists(pTextureName))
            {
                //! error texture does not exist
            }
            return mTextureLibrary[pTextureName];
        }
    }
}
